﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC3DEMO.Models;
using MVC3DEMO.Models.BS;

namespace MVC3DEMO.Controllers
{
    public class MenuController : Controller
    {
        //
        // GET: /Menu/
        private BS_Menu bs_menu;
        private SQLHeperDataContext sqlheper;
        public MenuController()
        {
            bs_menu = new BS_Menu();
            sqlheper = new SQLHeperDataContext();
        }
        public ActionResult Views()
        {
            var listMenu = bs_menu.getListMenu();

            return View(listMenu);
        }

        //
        // GET: /Menu/Details/5

        public ActionResult Details(int id)
        {
            var menu = bs_menu.getByMenuId(id);

            return View(menu);
        }

        //
        // GET: /Menu/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Menu/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                var menu = new Menu();
                menu.MenuName = collection["MenuName"];
                menu.priority =  int.Parse(collection["priority"].ToString().Trim());
                var show = collection["show"];
                if (show == "false")
                {
                    menu.show = false;
                }
                else
                {
                    menu.show = true;
                }

                sqlheper.asp_InsertMenu(menu.MenuName, menu.priority, menu.show);
                return RedirectToAction("Views");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Menu/Edit/5
 
        public ActionResult Edit(int id)
        {
            var menu = bs_menu.getByMenuId(id);
            return View(menu);
        }

        //
        // POST: /Menu/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                var menuId = bs_menu.getByMenuId(id);
                var menu = new Menu();
                menu.MenuName = collection["MenuName"];
                menu.priority = int.Parse(collection["priority"].ToString().Trim());
                var show = collection["show"];
                if (show == "false")
                {
                    menu.show = false;
                }
                else
                {
                    menu.show = true;
                }

                sqlheper.asp_UpdateMenu(menuId.Id, menu.MenuName, menu.priority, menu.show);
                return RedirectToAction("Views");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Menu/Delete/5
 
        public ActionResult Delete(int id)
        {
            var menu = bs_menu.getByMenuId(id);

            return View(menu);
        }

        //
        // POST: /Menu/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                sqlheper.asp_DeleteMenu(id);
 
                return RedirectToAction("Views");
            }
            catch
            {
                return View();
            }
        }

       
    }
}
